/**
 *
 * @authors garyzyz (zhouyouzhao@yeah.net)
 * @date    2016-11-30 15:06:31
 * 一个项目的公共变量，常量，
 * 以及http请求接口
 */

;(function (root, factory) {
  if (typeof exports === 'object' && typeof module === 'object') {
    module.exports = factory(exports);
  } else if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module.
    define(['exports'], factory);
  } else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {
    // CommonJS
    factory(exports);
  } else {
    // Browser globals
    root.conf = factory({});
  }
})(typeof window !== 'undefined' ? window : this, function (exports) {
  'use strict';
  function Config () {
    this._conf = {};
    merge(this._conf, this.constructor.conf);
  }

  Config.conf = {
    root: 'http://piao.pandauu.com/',
    apiRoot: 'http://piao.pandauu.com/',
    qRoot: 'http://localhost:8080/',
    // qRoot: 'http://test.pandauu.com/',
    // qApiRoot: 'http://test.pandauu.com/',
    qApiRoot: 'http://localhost:8080/',
    pRoot: 'http://piao.pandauu.com/',
    pApiRoot: 'http://piao.pandauu.com/',
    productUrl: 'http://piao.pandauu.com/single/piao_1_4/tx/productDetail.php?id=',
    dSize: 750,
    img: '/imgs',
    apiPre: {
      one: 'public/' //
    },

    /*
      http请求接口
    */

    api: {
      /*
        数据请求完整的api链接为：
          apiRoot + apiPre + api
      */
      /*
        hot
        get
        @param {INT} id
      */
      hot: 'hot' // 获取站点
    },

    /*
      http code
    */
    code: {
      ERR_OK: 0,
      ERR_NO_DATA: 107
    }
  };

  /*
    扩展_conf的函数
  */
  Config.prototype.extend = function (obj) {
    var target = this._conf;
    setThrow(arguments, 1, '只能传递一个参数！');

    for (var k in obj) {
      if (k === 'apiPre' || k === 'api') {
        for (var m in obj[k]) {
          target[k][m] = obj[k][m];
        }
      } else {
        target[k] = obj[k];
      }
    }
  };
  /*
    扩展http请求API前缀函数
  */

  Config.prototype.extendApiPre = function (obj) {
    var target = this._conf.apiPre;
    setThrow(arguments, 1, '只能传递一个参数！');

    merge(target, obj);
  };
  /*
    扩展http请求API函数
  */

  Config.prototype.extendApi = function (obj) {
    var target = this._conf.api;
    setThrow(arguments, 1, '只能传递一个参数！');

    merge(target, obj);
  };
  /*
    开发和测试环境切换
  */

  Config.prototype.switchPQ = function (toggle) {
    if (!toggle) {
      this._conf.root = this._conf.qRoot;
      this._conf.apiRoot = this._conf.qApiRoot;
    } else {
      this._conf.root = this._conf.pRoot;
      this._conf.apiRoot = this._conf.pApiRoot;
    }
  };
  /*
    根据数组长度报错
  */

  function setThrow (array, lenght, msg) {
    msg = msg || 'error';
    if (array.lenght > lenght) {
      throw (msg);
    }
  }
  /*
    内部两个对象合并函数
  */
  function merge (target, obj) {
    for (var k in obj) {
      target[k] = obj[k];
    }
  }
  var conf = new Config();
  /*
    false ---- development
    true ---- production
  */
  conf.switchPQ(true);
  return conf;
});
